package phone.qyg.com.qyg.activity;

import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.app.adapter.recycler.CommonAdapter;
import com.app.adapter.recycler.MultiItemTypeAdapter;
import com.app.adapter.recycler.RecyclerViewItemDecoration;
import com.app.adapter.recycler.base.ViewHolder;
import com.app.application.QXApplication;
import com.app.base.tool.BaseActivity;
import com.app.config.AppHttpKey;
import com.app.config.ApplicationConfig;
import com.app.utils.CommonUtil;
import com.app.utils.JSONTool;
import com.app.utils.PromptManager;
import com.app.utils.SystemPrintl;
import com.app.utils.TransformController;
import com.app.view.BaseViewStateLayout;
import com.app.xutils.MyCallBack;
import com.app.xutils.Xutils;

import org.xutils.view.annotation.ContentView;
import org.xutils.view.annotation.Event;
import org.xutils.view.annotation.ViewInject;

import java.util.List;

import phone.qyg.com.qyg.R;
import phone.qyg.com.qyg.model.Delete;
import phone.qyg.com.qyg.model.Message;
import phone.qyg.com.qyg.utils.AppHttpRequest;
import phone.qyg.com.qyg.utils.PopuDelete;

import static com.app.application.QXApplication.getContext;

/**
 * 作者：lblbh on 2017/12/5 18:30
 * 邮箱：lanbuhan@163.com
 * 功能：消息列表
 */
@ContentView(R.layout.msg)
public class MsgActivity extends BaseActivity {

    @ViewInject(R.id.back)
    TextView back;

    @ViewInject(R.id.all_delete)
    TextView all_delete;

//    @ViewInject(R.id.all_delete)
//    TextView all_delete;

    @ViewInject(R.id.linear)
    LinearLayout linear;

    RecyclerView msg_recycler;

    BaseViewStateLayout baseView;

    CommonAdapter adapter;

    List<Message> msgs;

    @Override
    public void initData(Bundle savedInstanceState) {


        View emptyView = View.inflate(getContext(), R.layout.base_view_state_empty, null);
        TextView center_text_view = (TextView) emptyView.findViewById(R.id.center_text_view);
        center_text_view.setText(R.string.no_app_msg);
        center_text_view.setVisibility(View.VISIBLE);
        baseView = new BaseViewStateLayout(this,emptyView) {
            @Override
            public View obtionView() {
                View sucess = View.inflate(QXApplication.getContext(), R.layout.base_recycler, null);
                sucess.setVisibility(View.VISIBLE);
                msg_recycler = (RecyclerView) sucess.findViewById(R.id.recycler);
                return sucess;
            }

            @Override
            public Object obtionData() {
                setRequestData();
                return DELFAUTSTATE;
            }
        };

        baseView.setVisibility(View.VISIBLE);
        ViewGroup.LayoutParams sucessParams = new ViewGroup.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT);
        if (baseView != null && linear != null) {
            linear.addView(baseView, sucessParams);
        }
    }

    private void setRequestData(){
        Xutils.Get(ApplicationConfig.NET_DSN_PATH + ApplicationConfig.MESS_LIST,
                null,
                new MyCallBack<String>(){
                    @Override
                    public void onSuccess(String result) {
                        super.onSuccess(result);
                        if(JSONTool.isStatus(result)){
                            if (JSONTool.isStatus(result)){
                                msgs = JSONTool.jsonDefaluTranClazzs(result, JSONTool.Enum.MESS_LIST,Message.class);

                                if(isRequestList(msgs)){
                                    baseView.setThreadOrState(BaseViewStateLayout.BaseViewState.STATE_SUCESS);
                                    baseView.stateView();
                                    setMsgData();
                                }else{
                                    baseView.setThreadOrState(BaseViewStateLayout.BaseViewState.STATE_EMPTY);
                                    baseView.stateView();
                                }
                            }else{
                                baseView.setThreadOrState(BaseViewStateLayout.BaseViewState.STATE_EMPTY);
                                baseView.stateView();
                            }
                        }
                    }

                    @Override
                    public void onError(Throwable ex, boolean isOnCallback) {
                        super.onError(ex, isOnCallback);
                        baseView.setThreadOrState(BaseViewStateLayout.BaseViewState.STATE_ERROE);
                        baseView.stateView();
                    }
                });
    }


    private void setMsgData(){
        if(!isRequestList(msgs)){
            all_delete.setVisibility(View.GONE);
            return;
        }

        adapter = new CommonAdapter<Message>(QXApplication.getContext(),R.layout.item_msg,msgs) {
            @Override
            protected void convert(ViewHolder holder, Message message, int position) {
                holder.setText(R.id.item_title,message.content);
                holder.setText(R.id.user_name,message.sendTime);
//                holder.setImage(R.id.item_img,message.,null);
                //0-未读，1-已读
                int isRead = message.isRead;
                int bg = (isRead == 0)? R.drawable.item_msg_select :R.drawable.item_msg_normal;
                holder.setBackgroundRes(R.id.item_icon,bg);
            }
        };
        adapter.setOnItemClickListener(new MultiItemTypeAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(View view, RecyclerView.ViewHolder holder, int position) {
                SystemPrintl.systemPrintl("我在点击的item---->>>>"+position);
                final Message msg = (Message) adapter.getData().get(position);
                TransformController.transformControllerModel(MsgActivity.this,MsgDetialsActivity.class,msg);

//                mapKeys.put(AppHttpKey.MESS_IDS,String.valueOf(msg.id));
//                AppHttpRequest.appQuitState(ApplicationConfig.UPDATE_MESSIS_READ,
//                        mapKeys,
//                        new AppHttpRequest.OnAppHttpListener() {
//                            @Override
//                            public void onAppHttpState(boolean isState, String result) {
//                                if(isState){
//                                    msg.isRead = 1;
//                                }
//                            }
//                        });
            }

            @Override
            public boolean onItemLongClick(View view, RecyclerView.ViewHolder holder, int position) {
                deleteMsg(position,false);
                return true;
            }
        });
        msg_recycler.setAdapter(adapter);
    }

    @Event(value = {R.id.back,R.id.all_delete})
    private void viewOnClick(View v){
        switch (v.getId()){
            case R.id.back:
                this.finish();
                break;
            //,R.id.all_delete
            case R.id.all_delete:
                deleteMsg(0,true);
                break;
        }
    }

    private void deleteMsg(final int position, final boolean isAll){
        if(!isRequestList(msgs))return;

        PopuDelete.showPopuWinDeleteCourse(back,
                new Delete(QXApplication.getContext().getResources().getString(R.string.delete_title),
                        ""),
                R.layout.popu_detele,
                position,
                new PopuDelete.DeleteState() {
                    @Override
                    public void state(boolean isSucess, int position) {
                        if(isSucess){
                            if(!isAll){
                                mapKeys.put(AppHttpKey.ID,String.valueOf(msgs.get(position).id));
                            }
                            mapKeys.put(AppHttpKey.TYPE,(isAll)?"2":"1");

                            /**
                             *   “type”:1 //删除类型  1、单个删除 2、全部清空
                             “id”:1 //消息ID，

                             */
                            AppHttpRequest.appQuitState(ApplicationConfig.DELETE_SYS_MESS,
                                    mapKeys,
                                    new AppHttpRequest.OnAppHttpListener() {
                                        @Override
                                        public void onAppHttpState(boolean isState, String result) {
                                            if(isState){
                                                PromptManager.showToast(R.string.delete_title_sucess);
                                                setRequestData();
                                            }
                                        }
                                    });

                        }
                    }
                });
    }


    @Override
    protected void onRestart() {
        super.onRestart();
        if(adapter != null)
            setRequestData();
//            adapter.notifyDataSetChanged();
    }

    @Override
    public void initSucessView() {}
    @Override
    public void initViewHandler() {
        if(msg_recycler == null)return;
        LinearLayoutManager layoutVerManager = new LinearLayoutManager(QXApplication.getContext());
        layoutVerManager.setOrientation(LinearLayoutManager.VERTICAL);
        msg_recycler.addItemDecoration(new RecyclerViewItemDecoration(
                RecyclerViewItemDecoration.MODE_HORIZONTAL,
                getResources().getColor(R.color.divider),
                CommonUtil.dip2px(QXApplication.getContext(),1),0,0));

        msg_recycler.setLayoutManager(layoutVerManager);
    }
}
